package com.yanqu.road.server;

import com.yanqu.road.NettyMgr;
import com.yanqu.road.dao.db.DBTableInfoMgr;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.server.agent.AgentInterfaceMgr;
import com.yanqu.road.server.manager.CrossDataAnalyticsMgr;
import com.yanqu.road.server.manager.activity.shuihuyezipai.ShuiHuYeZiCardActivityBattleMgr;
import com.yanqu.road.server.manager.activity.shuihuyezipai.ShuiHuYeZiCardActivityConfigMgr;
import com.yanqu.road.server.manager.chatcontrol.CrossChatControlMgr;
import com.yanqu.road.server.manager.config.*;
import com.yanqu.road.server.manager.cricketcourt.CrossCricketCourtMgr;
import com.yanqu.road.server.manager.kuangmatch.CrossKuangMatchMgr;
import com.yanqu.road.server.manager.log.CrossCenter2LogMgr;
import com.yanqu.road.server.manager.marriage.CrossUnionMarriageMgr;
import com.yanqu.road.server.manager.marriage.CrossUnionWorldMarriageMgr;
import com.yanqu.road.server.manager.player.WorldMessageMgr;
import com.yanqu.road.server.manager.rank.CrossGroupUserMonthAbilityRankMgr;
import com.yanqu.road.server.manager.systemgroup.Cross3SystemWeekGroupMatchMgr;
import com.yanqu.road.server.manager.thirdparty.PlatformMgr;
import com.yanqu.road.server.manager.union.*;
import com.yanqu.road.server.manager.union.activitygroup.Cross3UnionActivityGroupMgr;
import com.yanqu.road.server.manager.union.activitygroup.UnionActivityGroupLockMgr;
import com.yanqu.road.server.manager.union.honors.CrossUnionHonorsMgr;
import com.yanqu.road.server.manager.union.honors.CrossUnionHonorsRankMgr;
import com.yanqu.road.server.manager.union.huaben.CrossUnionHuaBenConfigMgr;
import com.yanqu.road.server.manager.union.huaben.CrossUnionHuaBenMgr;
import com.yanqu.road.server.manager.union.huaben.CrossUnionHuaBenRankMgr;
import com.yanqu.road.server.manager.union.trade.CrossUnionTradeMgr;
import com.yanqu.road.server.manager.union.unionrank.CrossUnionRankMsgCacheMgr;
import com.yanqu.road.server.manager.unioncampwar.CrossUnionCampWarLockGroupMgr;
import com.yanqu.road.server.manager.unioncampwar.CrossUnionCampWarMgr;
import com.yanqu.road.server.manager.user.CrossUserMgr;
import com.yanqu.road.server.manger.*;
import com.yanqu.road.server.timer.CenterCross2TimerMgr;
import com.yanqu.road.utils.config.Config;
import org.apache.logging.log4j.LogManager;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public final class CrossCenterServer2 extends CrossBaseServer {

    private static CrossCenterServer2 crossCenterServer;

    public static CrossCenterServer2 getInstance() {
        if (null == crossCenterServer) {
            crossCenterServer = new CrossCenterServer2();
            if (null == crossBaseServer) {
                crossBaseServer = crossCenterServer;
            }else {
                logger.error("CrossCenterServer2 getInstance baseServer exist");
            }
        }
        return crossCenterServer;
    }

    @Override
    public boolean stop() {
        try {
            super.stop();
            //保存mgr数据
            for (TempMgr tempMgr : CrossCenterServer2.getInstance().getMgrList()) {
                try {
                    tempMgr.save();
                }catch (Exception e){
                    logger.error("mgr save error:", e);
                }
            }
        } catch (Exception e) {
            logger.error("CrossCenterServer close error", e);
            return false;
        }

        return true;
    }

    public static void main(String[] args) {
        long time = System.currentTimeMillis();
        if (args.length <= 0) {
            System.err.println("check set config path");
            return;
        }

        Config.ParseArgs(args);
        cfgPath = args[0];

        CrossCenterServer2 crossCenterServer2 = CrossCenterServer2.getInstance();
        crossCenterServer2.setServerType(Integer.valueOf(args[1]));
        if (!crossCenterServer2.start(crossCenterServer2.getServerType(), CrossCenterServer2.class.getName())) {
            System.err.println("CrossCenterServer start fail!");
            System.exit(1);
            return;
        }

        DBTableInfoMgr dbTableInfoMgr = new DBTableInfoMgr();
        dbTableInfoMgr.init(DbNameStrategy.getCross2Db(), "com.yanqu.road.entity.log", true);
        crossCenterServer2.setDbTableInfoMgr(dbTableInfoMgr);

        CenterCross2TimerMgr.initGeneralTimers(crossCenterServer2.getServerType());
        System.err.println("CrossCenterServer2 start success");
        logger.info("CrossCenterServer2 start success,cost time:" + (System.currentTimeMillis() - time));
        TempServer.tempServerLogger.info("notify process start success!");


        Runtime.getRuntime().addShutdownHook(new Thread(()->{
            logger.info("CrossCenterServer2 关闭服务器");
            CrossCenterServer2.getInstance().newStop();
        },"ShutdownHook"));

    }

    public void newStop(){
        try {
            Thread.sleep(5000);
            // 关闭netty 避免有新连接
            NettyMgr.newStop();
            // 关闭定时器
            CenterCross2TimerMgr.stop();

            TempServer.getTimerExecutorService().shutdown();

            // 停止
            for (TempMgr tempMgr : getInstance().getMgrList()) {
                tempMgr.stop();
            }
            // 保存
            for (TempMgr tempMgr : getInstance().getMgrList()) {
                try {
                    tempMgr.save();
                } catch (Exception e) {
                    logger.error("mgr save error:", e);
                }
            }
            // 保存日志
            CrossCenter2LogMgr.save();

            logger.info("CrossCenterServer2 服务器已关闭");
            TempServer.deleteFile("./crossServer.pid");
            super.stopWithLogger();
            LogManager.shutdown();
            System.out.println("CrossCenterServer2 服务器已关闭");
        }catch (Exception e){
            logger.error("CrossCenterServer2 stop error", e);
        }
    }


	@Override
	public List<TempMgr> getMgrList() {

		List<TempMgr> mgrList = new ArrayList<>();
		//配置放前面
        mgrList.add(new CrossCheckConfigVersionMgr());
		mgrList.add(new ConfigMgr());
        mgrList.add(new ChannelConfigMgr(sType));
        mgrList.add(new CrossChannelSystemConfigMgr());
		mgrList.add(new ServerListMgr());
		mgrList.add(new ServerGroupMgr());
		mgrList.add(new CrossServerGroupMgr());
        mgrList.add(new CrossSystemSwitchMgr());
		mgrList.add(new AgentInterfaceMgr());
		mgrList.add(new ForbiddenWordMgr());
		mgrList.add(new CrossUnionNotifyMgr());
        mgrList.add(new CrossUnionServerGroupMgr());
        mgrList.add(new CrossUnionCampWarGroupMgr());
        mgrList.add(new CrossUnionConfigMgr());
        mgrList.add(new CrossUnionMgr());
        mgrList.add(new CrossUnionJoinApplyMgr());
        mgrList.add(new CrossUnionNoticeMgr());
        mgrList.add(new CrossUnionParergonMgr());
        mgrList.add(new CrossUnionTradeMgr());
        mgrList.add(new CrossUnionActivityLockRankMgr());
        mgrList.add(new SkinMgr());
        mgrList.add(new TempCommonMgr());
        mgrList.add(new TempGoodsBagMgr());
        mgrList.add(new GoodsMgr());
        mgrList.add(new VehicleMgr());
        mgrList.add(new PatronsMgr());
        mgrList.add(new PlatformMgr());
        mgrList.add(new DAServerListMgr());
        mgrList.add(new CrossDataAnalyticsMgr());
        mgrList.add(new KafKaHelperMgr());
        mgrList.add(new WorldMessageMgr());
        mgrList.add(new CrossUnionRankMgr());
        mgrList.add(new CrossChatControlMgr());
        mgrList.add(new CrossSystemLoopMgr());

        //分组Mgr
        mgrList.add(new CrossUserMgr());
        mgrList.add(new ShuiHuYeZiCardActivityConfigMgr());
        mgrList.add(new ShuiHuYeZiCardActivityBattleMgr());

        mgrList.add(new UnionHuaBenServerGroupMgr());
        mgrList.add(new CrossUnionHuaBenConfigMgr());
        mgrList.add(new CrossUnionHuaBenMgr());
        mgrList.add(new CrossUnionHuaBenRankMgr());
        mgrList.add(new CrossUnionHonorsMgr());
        mgrList.add(new CrossUnionHonorsRankMgr());
        mgrList.add(new CrossUnionMarriageMgr());
        mgrList.add(new CrossUnionWorldMarriageMgr());

        // 5.7版本废弃之前的蛐蛐赛季系统
//        mgrList.add(new CricketCenter2ServiceMgr());
        mgrList.add(new SkillMgr());
        mgrList.add(new CrossGroupUserMonthAbilityRankMgr());

        mgrList.add(new Cross3UnionActivityGroupMgr()); // 商会活动分组，放在UnionActivityGroupLockMgr之前
        mgrList.add(new UnionActivityGroupLockMgr());

        mgrList.add(new CricketServerGroupMgr());
        mgrList.add(new CrossCricketCourtMgr());

        mgrList.add(new CrossKuangMatchMgr());
        mgrList.add(new Cross3SystemWeekGroupMatchMgr());

        mgrList.add(new CrossUnionCampWarLockGroupMgr());
        mgrList.add(new CrossUnionCampWarMgr());

        mgrList.add(new CrossUnionRankMsgCacheMgr());

		return mgrList;
	}
}
